package com.example.boot.factorycase;

import com.example.boot.dto.PreCheckDto;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;

@Slf4j
@Data
public abstract class AbstractPreCheckScoreCollector implements PreCheckScoreCollector {

    protected ThreadLocal<PreCheckDto> preCheckDto = ThreadLocal.withInitial(PreCheckDto::new);

    @Override
    public String process(String score) {
        try {
            log.info("{}-写={}", Thread.currentThread().getName(), score);
            writeScore(score);
            Thread.sleep(new Random().nextInt(1000));
            String value = readScore();
            log.info("{}-读={}", Thread.currentThread().getName(), value);
        } catch (Exception e) {
            log.error("执行异常", e);
        }
        return "Y";
    }

    /**
     * 属性构建
     */
    public void buildParams(String score, String supplier) {
        preCheckDto.set(new PreCheckDto(score, supplier));
    }

    /**
     * 写属性
     */
    protected abstract void writeScore(String score);

    /**
     * 读属性
     */
    protected abstract String readScore();

}
